﻿/*************************************************************************
 * Created by OhZee Creative, LLC
 * 2013
 * This code is licensed under Microsoft Public License (Ms-PL)
 * http://opensource.org/licenses/ms-pl
 * http://ozframework.codeplex.com/
 *************************************************************************/

namespace OhZeeCreative
{
    using System;
    using System.ComponentModel;
    using System.Windows.Input;

    public abstract class NotifyingDelegateCommandBase : ICommand, INotifyPropertyChanged
    {
        public NotifyingDelegateCommandBase(bool isInitiallyEnabled = false)
        {
            this.isEnabled = isInitiallyEnabled;
        }

        private bool isEnabled;
        public bool IsEnabled
        {
            get { return this.isEnabled; }
            set
            {
                if (this.isEnabled != value)
                {
                    this.isEnabled = value;
                    if (this.CanExecuteChanged != null)
                    {
                        this.CanExecuteChanged(this, EventArgs.Empty);
                    }
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("IsEnabled"));
                    }
                }
            }
        }

        public virtual bool CanExecute(object parameter)
        {
            return this.IsEnabled;
        }

        public event EventHandler CanExecuteChanged;

        public abstract void Execute(object parameter);

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class NotifyingDelegateCommand : NotifyingDelegateCommandBase
    {
        private Action action;

        public NotifyingDelegateCommand(Action action, bool isInitiallyEnabled = false) : base(isInitiallyEnabled)
        {
            this.action = action;
        }

        public override void Execute(object parameter)
        {
            if (this.CanExecute(parameter))
            {
                this.action();
            }
        }
    }

    public class NotifyingDelegateCommand<T> : NotifyingDelegateCommandBase
    {
        private Action<T> action;

        public NotifyingDelegateCommand(Action<T> action, bool isInitiallyEnabled = false)
            : base(isInitiallyEnabled)
        {
            this.action = action;
        }

        public override void Execute(object parameter)
        {
            if (this.CanExecute(parameter))
            {
                this.action((T) parameter);
            }
        }
    }





}
